﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace PTA.NET
{
    public partial class FrmNhapAnswer : DevExpress.XtraEditors.XtraForm
    {
        public string SelectedImageKey;

        public Customer CustomerItem;

        public delegate void AnswerItemEventHandler(int AnswerType, int Diem);
        public event AnswerItemEventHandler AnswerItemClicked;
        void RaiseAnswerItemClicked(int AnswerType, int Diem)
        {
            if (AnswerItemClicked != null)
                AnswerItemClicked(AnswerType, Diem);
        }


        public FrmNhapAnswer()
        {
            InitializeComponent();
        }

        

        
        private void FrmNhapAnswer_Load(object sender, EventArgs e)
        {            
            CustomerItem = new Customer();
            BuildListViewAnswers();
            
        }

        private void BuildListViewAnswers()
        {
            string[] data = File.ReadAllLines("Data\\cauhoi.txt");
            List<Questions> listQuestion = new List<Questions>();

            for (int index = 1; index < data.Count(); index++)
            {
                Questions q = new Questions(index, data[index].Split('#')[1], "");
                CustomerItem.QuestionList[index] = q;
                listQuestion.Add(q);
            }

            

            // Set ICOn
            this.olvColumn1.ImageGetter = delegate(object row)
            {

                int stt = ((Questions)row).STT;

                string answer = CustomerItem.QuestionList[stt].Answer;
                //string answer = olvColumn3.GetStringValue(row);

                if (answer == null || answer == "") return null;

                if (answer.Length > 0 && answer.Substring(0, 1).Contains("D"))
                    return 1;
                else if (answer.Length > 0 && answer.Substring(0, 1).Contains("S"))
                    return 0;
                else
                    return 2;
            };

            

            lsvAnswers.DataSource = listQuestion;

        }


        private void lsvAnswers_SelectedIndexChanged(object sender, EventArgs e)
        {
            int Diem = 1;
            if (((BrightIdeasSoftware.ObjectListView)(sender)).SelectedIndex >= 0)
            {
                int stt = ((BrightIdeasSoftware.ObjectListView)(sender)).SelectedIndex + 1;

                if (CustomerItem.QuestionList[stt].Answer == SelectedImageKey) return;
                //if (((BrightIdeasSoftware.ObjectListView)(sender)).SelectedItem.ImageKey != "")
                if (!string.IsNullOrEmpty(CustomerItem.QuestionList[stt].Answer))
                {
                    // Trùng câu trả lời
                    
                    
                    if (DevExpress.XtraEditors.XtraMessageBox.Show("Câu hỏi này đã có trả lời. Bạn muốn thay thế không?", "Chú ý", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == System.Windows.Forms.DialogResult.No)
                        return;
                    else
                    {
                        switch (CustomerItem.QuestionList[stt].Answer)
                        //switch (SelectedImageKey)
                        {
                            case "DUNG":
                                RaiseAnswerItemClicked(0, -1);
                                break;

                            case "SAI":
                                RaiseAnswerItemClicked(1, -1);
                                break;
                            case "KHONG":
                                RaiseAnswerItemClicked(2, -1);
                                break;
                        }

                        //CustomerItem.QuestionList[stt].Answer = SelectedImageKey;
                        //Diem = -1;
                    }
                }

                
                CustomerItem.QuestionList[stt].Answer = SelectedImageKey;



                //((BrightIdeasSoftware.ObjectListView)(sender)).SelectedItem.SubItems[3].Tag = SelectedImageKey;
                //((BrightIdeasSoftware.ObjectListView)(sender)).SelectedItem.SubItems[3].Text = SelectedImageKey;



                // Doi icon
                 ((BrightIdeasSoftware.ObjectListView)(sender)).SelectedItem.ImageKey = SelectedImageKey;
                 lsvAnswers.Items[((BrightIdeasSoftware.ObjectListView)(sender)).SelectedIndex].ImageKey = SelectedImageKey;

               // ((BrightIdeasSoftware.ObjectListView)(sender)).SelectedItem.ImageKey = SelectedImageKey;
               // lsvAnswers.Items[((BrightIdeasSoftware.ObjectListView)(sender)).SelectedIndex].ImageKey = SelectedImageKey;
               //// ((BrightIdeasSoftware.OLVColumn)(lsvAnswers.SelectedItem.SubItems[0].).ImageKey = SelectedImageKey;

                 lsvAnswers.Refresh();
                 lsvAnswers.RefreshObject(((BrightIdeasSoftware.ObjectListView)(sender)).SelectedObject);
                
                switch (SelectedImageKey)
                {
                    case "DUNG":
                        RaiseAnswerItemClicked(0, Diem);                        
                        break;

                    case "SAI":
                        RaiseAnswerItemClicked(1, Diem);
                        break;
                    case "KHONG":
                        RaiseAnswerItemClicked(2, Diem);
                        break;
                }
            }
        }

        public void ChangeViewType(int Type)
        {
            if (Type == 0)
                lsvAnswers.View = View.Details;
            else if (Type == 1)
                lsvAnswers.View = View.LargeIcon;
            else if (Type == 2)
                lsvAnswers.View = View.List;
            else if (Type == 3)
                lsvAnswers.View = View.SmallIcon;
            else if (Type == 4)
                lsvAnswers.View = View.Tile;
        }

        private void aqButtonX1_Click(object sender, EventArgs e)
        {
            //int L = CauTaoCacThang.TinhDiem2(CustomerItem.QuestionList, CauTaoCacThang.F, (int)TRA_LOI.DUNG);
        }

   


       
    }
}
